package com.atguigu.lease.app.service.impl;

import com.atguigu.lease.app.mapper.*;
import com.atguigu.lease.app.service.BrowsingHistoryService;
import com.atguigu.lease.app.service.GraphInfoService;
import com.atguigu.lease.app.service.RoomInfoService;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.model.bean.*;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.apartment.AttrValueVo;
import com.atguigu.lease.model.vo.apartment.FeeValueVo;
import com.atguigu.lease.model.vo.apartment.GraphVo;
import com.atguigu.lease.model.vo.app.ApartmentItemVo;
import com.atguigu.lease.model.vo.app.RoomDetailVo;
import com.atguigu.lease.model.vo.app.RoomItemVo;
import com.atguigu.lease.model.vo.app.RoomQueryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YouKinn
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2025-06-20 20:18:17
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Resource
    ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    GraphInfoService graphInfoService;
    @Resource
    RoomAttrValueMapper roomAttrValueMapper;
    @Resource
    RoomFacilityMapper roomFacilityMapper;
    @Resource
    RoomLabelMapper roomLabelMapper;
    @Resource
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Resource
    ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Resource
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Resource
    BrowsingHistoryService browsingHistoryService;

    @Override
    public Page<RoomItemVo> pageItem(Long current, Long size, RoomQueryVo queryVo) {
        // ======不用：分多步查询： 遍历分页的房间列表 然后一个个的去查找，效率低

        //自定义分页查询的sql：
        return baseMapper.selectRoomItemVoPageItem(new Page<RoomItemVo>(current, size),
                queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //分多步查询： 为RoomItemVo查询需要的数据组装

        //1、查询房间+入住状态信息设置
        RoomDetailVo roomDetailVo =  baseMapper.selectRoomDetailVoById(id);

        //2、设置公寓信息:ApartmentItemVo
        ApartmentItemVo apartmentItemVo = apartmentInfoMapper
                .selectApartmentItemVo(roomDetailVo.getApartmentId());
        roomDetailVo.setApartmentItemVo(apartmentItemVo);


        //3、设置房间的图片列表：GraphVo
        List<GraphVo> graphVoList = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemTypeEnum.ROOM))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());

        roomDetailVo.setGraphVoList(graphVoList);
        //4、设置房间的属性值列表：AttrValueVo
        List<AttrValueVo> attrValueVos = roomAttrValueMapper.selectAttValueVosByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        //5、设置房间的配套列表：FacilityInfo
        List<FacilityInfo> facilityInfos = roomFacilityMapper.selectFacilityInfosByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        //6、设置房间的标签列表：LabelInfo
        List<LabelInfo> labelInfos = roomLabelMapper.selectLabelInfosByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfos);

        //7、设置房间的支付方式列表：PaymentType
        List<PaymentType> paymentTypes = roomPaymentTypeMapper.selectPaymentTypesByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        //8、设置房间所在公寓的杂费值列表：FeeValueVo
        List<FeeValueVo> feeValueVos  = apartmentFeeValueMapper
                .selectFeeValueVoList(roomDetailVo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVos);
        //9、设置房间的租期列表：LeaseTerm
        List<LeaseTerm> leaseTerms = roomLeaseTermMapper.selectLeaseTermsByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTerms);

        browsingHistoryService.saveBrowsingHistoryAsyc(id ,
                StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId());


        return roomDetailVo;
    }

    @Override
    public Page<RoomItemVo> pageItemByApartmentId(Long id, Long current, Long size) {
        return baseMapper.selectRoomItemVoPageItemByApartmentId(new Page<RoomItemVo>(current,size),
                id);
    }

}





